・フォームに ”Hellow World !!" を表示する
・DOS窓に”Hellow World !!”を表示する。
■ フォームに ”Hellow World !!" を表示する
| フォームへの定番の”Hellow World !!” |
実行結果 | |
public Form1()
{
InitializeComponent();
label1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Hellow World !!";
}
|
![]() |
|
■ DOS窓(コンソールウィンドウ)に”Hellow World !!”を表示する。
| DOS窓での定番”Hellow world !!”。 [ファイル]→[新規作成]→[プロジェクト]→[C#]→[コンソールアプリケーション]から作成します。 DOS画面がプログラム終了後自動的にウィンドウが閉じないように、[デバック]→[デバックなし開始]で実行します。 |
実行結果 | |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleHellow
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World !!");
}
}
}
|
![]() |
|
| 備考 Consoleクラスのメソッド .Read() Write() ReadLine() Beep() Clear() 他 |
<追記>
類似のプロジェクトをコピーして作成する場合は、プロジェクト名だけ変更してソリューション名などは変更しないで利用する。 エクスプローラや
ソリューションエクスプローラから ソリューション名やNameSpaceなどをよく理解しないで変更するといろいろ面倒なことが発生します。
| 実行結果 | ||
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace messageBOX
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show
("ボタンの色を黄色に変えますか?",
"質問",
MessageBoxButtons.YesNoCancel, //YesNo
MessageBoxIcon.Information, //アイコンのデザイン選択 Error: X Exclamation: !
MessageBoxDefaultButton.Button2); //フォーカスの位置 //推奨ボタン
//何が選択されたか調べる
if (result == DialogResult.Yes)
{
button1.BackColor = Color.Yellow;
}
else if (result == DialogResult.No)
{
}
else if (result == DialogResult.Cancel)
{
}
}
}
}
|
![]() |
| ・フォーム追加後、旧フォームを非表示にする ・追加フォームを閉じて、旧フォームを表示する |
実行結果 | |
<プログラム例>
//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace formClose
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this); //Form2にForm1の参照を渡す
form2.Show(); //フォーム2を表示
this.Hide(); //フォーム1を非表示
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Application.Exit(); //アプリケーション終了
}
}
}
//Foprm2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace formClose
{
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 form1) //Form1の参照を受け取る
{
InitializeComponent();
f1 = form1; //form1を代入
}
private void button1_Click(object sender, EventArgs e)
{
f1.Show(); //フォーム1を表示する
this.Hide(); //フォーム2を非表示にする
}
}
}
|
![]() ![]() |
| タイマコンポーネントをフォームに貼り付けます。経過時間後のイベントで タイマを停止します。 下記は2秒後に別フォームが立ち上がるプログラムです |
実行結果 | |
<プログラム例>
//Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace formTimer_change
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Tset();
}
private void Tset()
{
label1.Text = "1番目";
timer1.Interval = 2000; //経過時間(2秒)設定
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false; //タイマ1停止
Form2 form2 = new Form2(); //ファーム2オブジェクト生成
form2.ShowDialog(); //フォーム2表示
}
}
}
//-----------------------------------------------------
//Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace formTimer_change
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
label1.Text = "2番目";
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
|
![]() |
| レジストリからの読出しは、フォルダが存在しない場合、値がない場合、値が 期待するものでない場合等を加味してtry、catch構文が必要となります。 下記はレジストリから整数のみを読みだすプログラムです。 |
実行結果 | |
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32; //レジストリ読み書きには必須
namespace ResistryRW
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //レジストリへ書込み
{
RegistryKey myKey = Registry.CurrentUser;
myKey = myKey.CreateSubKey(@"ys-labo\testFolder");
myKey.SetValue("Str1", textBox1.Text);
}
private void button2_Click(object sender, EventArgs e) //レジストリの読出し
{
int itemp;
try //別のPCから起動するかわる時などでは必須
{
RegistryKey myKey = Registry.CurrentUser;
myKey = myKey.OpenSubKey(@"ys-labo\testFolder");
string data = (string)myKey.GetValue("Str1");
bool result = Int32.TryParse(data, out itemp);
if (result) textBox2.Text = itemp.ToString();
else MessageBox.Show("整数ではありません");
myKey.Close();
}
catch
{
MessageBox.Show("ys-labo\\testFolder のレジストリフォルダが存在しません");
}
}
private void button3_Click(object sender, EventArgs e) //レジストリ削除
{
try
{
RegistryKey myKey = Registry.CurrentUser;
myKey = myKey.OpenSubKey(@"ys-labo\testFolder", true);
myKey.DeleteValue("Str1");
MessageBox.Show("レジストリからStr1を削除");
}
catch
{
MessageBox.Show("レジストリにStr1はありませんでした",
"報告",
MessageBoxButtons.OK);
}
}
}
}
|
![]() |
| タスクバーのないフォーム(スタートボタン有) 他のアプリケーションを起動できないようにする場合に有効です |
実行結果 | |
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //API関数を使用する場合必要
namespace No_taskbar
{
public partial class Form1 : Form
{
private bool BtnMode = false;
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(Int32 hWnd, int nCmdShow);
[DllImport("User32.dll")]
public static extern Int32 FindWindowEx(Int32 hWndParent, IntPtr hWndChild, String lpClassName, String lpWindowName);
const int SW_HIDE = 0;
const int SW_SHOWNORMAL = 1;
const int SW_NORMAL = 1;
const int SW_SHOWMINIMIZED = 2;
const int SW_SHOWMAXIMIZED = 3;
const int SW_MAXIMIZE = 3;
const int SW_SHOWNOACTIVATE = 4;
const int SW_SHOW = 5;
const int SW_MINIMIZE = 6;
const int SW_SHOWMINNOACTIVE = 7;
const int SW_SHOWNA = 8;
const int SW_RESTORE = 9;
const int SW_SHOWDEFAULT = 10;
const int SW_FORCEMINIMIZE = 11;
const int SW_MAX = 11;
public Form1()
{
InitializeComponent();
// this.FormBorderStyle = FormBorderStyle.None; //タイトルバー& タスクバー 非表示最大化
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Maximized;//
// コントロールボックスを非表示
// this.ControlBox = false;
//タイトルバーのテキストを消す
this.ControlBox = true;
this.Text = "";
//this.MaximizeBox = false; //最大化ボタン非表示
//this.MinimizeBox = false;
//this.HelpButton = false;
}
//タスクバー表示・非表示処理
//mode=1:表示,=2:非表示
public static void setTaskbarStyle(int mode)
{
Int32 hWnd = FindWindow("Shell_TrayWnd", null);
if (hWnd != 0)
{
if (mode == 1)ShowWindow(hWnd, SW_SHOW);//表示
else ShowWindow(hWnd, SW_HIDE);//非表示
}
}
private void button1_Click(object sender, EventArgs e)
{
if (BtnMode)
{
BtnMode = false;
setTaskbarStyle(1);//タスクバー表示
}
else
{
BtnMode = true;
setTaskbarStyle(2); //タスクバー非表示
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
setTaskbarStyle(1);//タスクバー表示
}
}
}
|
![]() |
|
| タイトルバー、タスクバーのないフォーム(スタートボタンなし) 他のアプリケーションを起動できないようにする場合に有効です |
実行結果 | |
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None; //タイトルバー& タスクバー 非表示最大化
this.WindowState = FormWindowState.Maximized;//
}
}
}
|
![]() |
| 実行結果 | ||
|
<プログラム例>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace doubleStart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String pname = Process.GetCurrentProcess().ProcessName;
Process[] prvc = Process.GetProcessesByName(pname);
if (prvc.Length > 1) //
{
MessageBox.Show("すでに本ソフトは起動しています");
this.Close();
}
}
}
}
|
![]() |
| 参照渡しをつかって値をスワップした例です。 | 実行結果 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 参照渡し
{
public partial class Form1 : Form
{
int a = 1;
int b = 2;
public Form1()
{
InitializeComponent();
label1.Text = "a = " + a.ToString();
label2.Text = "b = " + b.ToString();
}
static void swap(ref int a0, ref int b0) //参照内容のスワップ
{
int temp;
temp = a0;
a0 = b0;
b0 = temp;
}
private void button1_Click(object sender, EventArgs e)
{
swap(ref a,ref b); //a,b の参照を渡す //ref = &
label1.Text = "a = " + a.ToString();
label2.Text = "b = " + b.ToString();
}
}
}
|
<実行前>![]() <実行後> ![]() |